﻿using Abp.Extensions;
using Sunlit.House.Buy;
using Sunlit.House.Common.Dto;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Sunlit.House.Articles;
using Sunlit.House.Articles.Dto;
using Sunlit.House.Floors.Dto;
using Sunlit.House.Floors;
using Sunlit.House.Seconds;
using Sunlit.House.Seconds.Dto;
using Abp.Domain.Repositories;
using Abp.Linq.Extensions;
using Microsoft.EntityFrameworkCore;

namespace Sunlit.House.Common
{
    public class CommonAppService:HouseAppServiceBase,ICommonAppService
    {
        private readonly IRepository<News, long> _newsRepository;
        private readonly IRepository<Floor, long> _floorRepository;
        private readonly IRepository<SecondHand, long> _secondRepository;

        public CommonAppService(
            IRepository<News, long> newsRepository,
            IRepository<Floor, long> floorRepository,
            IRepository<SecondHand, long> secondRepository
            )
        {
            _floorRepository = floorRepository;
            _newsRepository = newsRepository;
            _secondRepository = secondRepository;
        }
        private GetEnumOutput GetEnum<T>()
        {
            GetEnumOutput output = new GetEnumOutput();
            Type type = typeof(T);
            foreach (FieldInfo field in type.GetFields(BindingFlags.Static | BindingFlags.Public))
            {
                var curDesc = field.GetCustomAttributes(typeof(DescriptionAttribute), false)
                    .FirstOrDefault()
                    .As<DescriptionAttribute>();
                var label = curDesc?.Description;
                var value = (int)field.GetValue(null);
                output.SelectItems.Add(new LabelValueDto { Label = label, Value = value });
                output.SelectItemDic.Add(value, label);
            }
            return output;
        }
        private GetEnumOutput GetEnumOrigin<T>()
        {
            GetEnumOutput output = new GetEnumOutput();
            Type type = typeof(T);
            foreach (int v in Enum.GetValues(type))
            {
                var label = Enum.GetName(type, v);
                var value = v;
                output.SelectItems.Add(new LabelValueDto { Label = label, Value = value });
                output.SelectItemDic.Add(value, label);
            }
            return output;
        }
        public Dictionary<string,GetEnumOutput> GetAllEnumTypes()
        {
            Dictionary<string, GetEnumOutput> dic = new Dictionary<string, GetEnumOutput>();

            var groupState = GetEnum<GroupState>();
            dic.Add("groupState", groupState);

            var sliderType = GetEnum<SliderType>();
            dic.Add("sliderType", sliderType);

            return dic;
        }
        public async Task<List<SliderDto>> GetSliderAsync()
        {
            var list = await _newsRepository.GetAll()
                .Where(n => n.InSlider)
                .Select(n => new SliderDto
                {
                    ImageUrl = n.ImageUrl,
                    Title = n.Title,
                    SliderType = SliderType.News,
                    Id = n.Id
                }).ToListAsync();
            var listFloor = await _floorRepository.GetAll()
                .Include(f => f.FloorImages)
                .Where(n => n.InSlider)
                .Select(n => new SliderDto
                {
                    ImageUrl = n.FloorImages.Count > 0 ? n.FloorImages.First().ImageUrl : "",
                    Title = n.Title,
                    SliderType = SliderType.Floor,
                    Id = n.Id
                }).ToListAsync();
            list.AddRange(listFloor);

            var listSecond = await _secondRepository.GetAll()
                .Include(f => f.SecondHandImages)
                .Where(n => n.InSlider)
                .Select(n => new SliderDto
                {
                    ImageUrl = n.SecondHandImages.Count > 0 ? n.SecondHandImages.First().ImageUrl : "",
                    Title = n.Title,
                    SliderType = SliderType.SecondHand,
                    Id = n.Id
                }).ToListAsync();
            list.AddRange(listSecond);

            return list;
        }
    }
}
